<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
.STYLE1 {
	font-size: x-large;
	font-weight: bold;
}
.STYLE2 {color: #FF0000}
-->
</style>
</head>

<?PHP
session_start();
include("../config/connect.php");
include("../public/common.php");
include("../public/biaoqing.php");

if( !isset($_SESSION["email"]) || $_SESSION["right"] < 1000) 
{
	$url = "Location:login.php?ref=edit_index_catelog.php";
	header($url);
}

$userid = $_SESSION["userid"];

$opt = CheckUrlParamNoDie("opt");
$id = CheckUrlParamNoDie("id");

if ($opt == "save" && $id != "")
{
	$name = GetPostParamStr("name");
	$picurl = mysql_real_escape_string(GetPostParamStr("picurl"));
	$order = mysql_real_escape_string(GetPostParamStr("order"));
//	echo "update tsj_home_catelog set name='$name',pic_url='$picurl',`order`=$order where id=$id";
//	exit;
	mysql_query("update tsj_home_catelog set name='$name',pic_url='$picurl',`order`=$order where id=$id");
	if (mysql_affected_rows() == 0)
		echo "<script>alert('数据未更新！');</script>";
	header("Location:?");
}
else if ($opt == "get_pic_all")
{
	$result = mysql_query( "select * from tsj_home_catelog where type=1 order by `order`" );
	while( $result && $row = mysql_fetch_assoc( $result ))
	{
		$picurl = mysql_real_escape_string(GetPic($row["parent_name"], $row["name"]));
//		echo "update tsj_home_catelog set pic_url='$picurl' where id=".$row["id"];
		mysql_query("update tsj_home_catelog set pic_url='$picurl' where id=".$row["id"]);
	}
	
	header("Location:?");
}
else if ($opt == "get_pic_one" && $id != "")
{
	$name = urldecode(CheckUrlParamNoDie("name"));
	$parentname = urldecode(CheckUrlParamNoDie("parentname"));
	$picurl = mysql_real_escape_string(GetPic($parentname, $name));
//	echo "update tsj_home_catelog set pic_url='$picurl' where id=$id";
//	exit;
	mysql_query("update tsj_home_catelog set pic_url='$picurl' where id=$id");
	if (mysql_affected_rows() == 0)
		echo "<script>alert('数据未更新！');</script>";
	else
		header("Location:?");
}

function GetPic($cl,$tag)
{
	$result = mysql_query( "select id,pic_url from goods_items where sys_tag='$cl' and tag like '%$tag%' order by rand(now()) limit 1" );
//	echo "select id,pic_url from goods_items where sys_tag='$cl' and tag like '%$tag%' order by rand(now()) limit 1";
	if ($result)
	{
		$rowTemp = mysql_fetch_assoc($result);
		return $rowTemp["pic_url"];
	}
	
	return "http://img02.taobaocdn.com/bao/uploaded/i2/T13VF_XoNqXXaaLHA__075454.jpg.jpg";
}
?>

<body>
<!--<a href="add_trade.php?opt=add">添加记录</a>-->
<div align="center" class="STYLE1">首页目录编辑后台 <span class="STYLE2">当前用户：</span><?PHP echo $_SESSION["email"]; ?><span style="padding-left:10px;"><a href="logout.php">退出</a></span></div>
<br>
<a href="?opt=get_pic_all">更新所有图片</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.tusijie.com/?refresh">更新首页缓存</a>
</p>

<?PHP
$result0 = mysql_query( "select * from tsj_home_catelog where type=0 order by `order`" );
while( $result0 && $row0 = mysql_fetch_assoc( $result0 ))
{
	$i = 0;
?>
<br />
<?PHP echo $row0["name"]; ?>-------大图
<hr />
<table width="100%" cellpadding="0" cellspacing="0" border="1" align="center">
	<tr bgcolor="#FFCC66">
    	<td height="25" width="50" align="center">ID</td>
        <td width="160" align="center">名称</td>
        <td width="80" align="center">图片URL</td>
   	  	<td width="60" align="center">顺序</td>
      	<td width="200" align="center">预览</td>
        <td width="30" align="center">操作</td>
   	</tr>

<?php
$result1 = mysql_query( "select * from tsj_home_catelog where type=1 and parent_name='".$row0["name"]."' order by `order`" );
while( $result1 && $row1 = mysql_fetch_assoc( $result1 ))
{
?>
<form action="?opt=save&id=<?PHP echo $row1["id"]; ?>" method="post">
<tr <?PHP if ($i++ % 2 == 0) echo "bgcolor='#CCCCCC'";?> >
    <td height="25" width="50" align="center"><?PHP echo $i; ?></a></td>
    <td align="center" ><input name="name" type="text" id="name" value="<?PHP echo $row1["name"]; ?>" size="20" /></td>
    <td align="center" ><input name="picurl" type="text" id="picurl" value="<?PHP echo $row1["pic_url"]; ?>" size="68" /></td>
    <td align="center" ><input name="order" type="text" id="order" value="<?PHP echo $row1["order"]; ?>" size="10" /></td>
    <td align="center" ><img src="<?PHP echo $row1["pic_url"]."_80x80.jpg"; ?>" /></td>
    <td align="center" ><input type="submit" value="保存" />&nbsp;&nbsp;<a href="?opt=get_pic_one&id=<?PHP echo $row1["id"]; ?>&name=<?PHP echo urlencode($row1["name"]); ?>&parentname=<?PHP echo urlencode($row1["parent_name"]); ?>">更新图片</a></td>
  </tr>
</form>
<?PHP
}
?>
</table>
<?PHP echo $row0["name"]; ?>-------文字
<hr />
<table width="100%" cellpadding="0" cellspacing="0" border="1" align="center">
	<tr bgcolor="#FFCC66">
    	<td height="25" width="50" align="center">ID</td>
        <td width="160" align="center">名称</td>
   	  	<td width="60" align="center">顺序</td>
      	<td width="30" align="center">操作</td>
   	</tr>

<?php
$i = 0;
$result1 = mysql_query( "select * from tsj_home_catelog where type=2 and parent_name='".$row0["name"]."' order by `order`" );
while( $result1 && $row1 = mysql_fetch_assoc( $result1 ))
{
?>
<form action="?opt=save&id=<?PHP echo $row1["id"]; ?>" method="post">
<tr <?PHP if ($i++ % 2 == 0) echo "bgcolor='#CCCCCC'";?> >
    <td height="25" width="50" align="center"><?PHP echo $i; ?></a></td>
    <td align="center" ><input type="text" id="name" name="name" value="<?PHP echo $row1["name"]; ?>" /></td>
    <td align="center" ><input name="order" type="text" id="order" value="<?PHP echo $row1["order"]; ?>" size="10" /></td>
    <td align="center" ><input type="submit" value="保存" /></td>
  </tr>
</form>
<?PHP
}
?>
</table>

<?PHP
}
?>

</body>
</html>
